import { defineStore } from 'pinia';

export const useMessagesStore = defineStore('messages', {
    state: () => ({
        messages: JSON.parse(localStorage.getItem('messages') || '[]') as { id: number; text: string }[],
    }),
    actions: {
        addMessage(message: { id: number; text: string }) {
            this.messages.push(message);
            localStorage.setItem('messages', JSON.stringify(this.messages));
        },
        getMessageById(id: number) {
            return this.messages.find(msg => msg.id === id);
        },
        clearMessages() {
            this.messages = [];
            localStorage.removeItem('messages');
        },
    },
});